<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="事项名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入事项名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
          @clear="resetQuery"
        />
      </el-form-item>
      <el-form-item label="事项类型" prop="itemType">
        <el-select
          v-model="queryParams.itemType"
          clearable
          :empty-values="[null]"
          :value-on-clear="null"
          placeholder="请选择事项类型"
          @clear="resetQuery"
          @change="handleQuery"
          style="width: 220px"
        >
          <el-option
            v-for="item in type_options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['workGuide:add']"
          >添加业务办理信息</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="tableList" border>
      <el-table-column label="序号" type="index" align="center" width="70" />
      <el-table-column
        label="事项名称"
        prop="itemName"
        show-overflow-tooltip
        align="center"
      />
      <el-table-column label="事项类型" align="center" prop="itemType" />
      <el-table-column label="内容" prop="content" align="center">
        <template #default="scope">
          <div v-html="scope.row.content"></div>
        </template>
      </el-table-column>
      <el-table-column label="是否常用事项" align="center" prop="status">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.status == 1">是</el-tag>
          <el-tag type="info" v-if="scope.row.status == 0">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="LOGO" align="center">
        <template #default="scope">
          <el-image
            style="width: 60px; height: 60px; margin-bottom: -5px"
            :src="baseUrl + scope.row.logoUrl"
            :preview-src-list="[baseUrl + scope.row.logoUrl]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="viewCount" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            text
            @click="handleDetail(scope.row)"
            v-hasPermi="['workGuide:view']"
          >
            查询
          </el-button>

          <el-button
            link
            type="primary"
            text
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workGuide:edit']"
          >
            编辑
          </el-button>

          <el-button
            link
            type="primary"
            text
            @click="handleDelete(scope.row)"
            v-hasPermi="['workGuide:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="40vw"
      align-center
      append-to-body
    >
      <el-form
        ref="workGuideRef"
        :model="form"
        :rules="rules"
        label-width="110px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="事项名称" prop="itemName" required>
              <el-input v-model="form.itemName" placeholder="请输入事项名称" />
            </el-form-item>
            <el-form-item label="事项类型" prop="itemType" required>
              <el-select
                v-model="form.itemType"
                clearable
                placeholder="请选择事项类型"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="办理地址" prop="processingAddress">
              <el-input v-model="form.processingAddress" placeholder="请输入办理地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否常用事项" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio :value="1">是</el-radio>
                <el-radio :value="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="LOGO" prop="logoUrl">
              <image-upload
                v-model="form.logoUrl"
                :isShowTip="false"
                :limit="1"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容" prop="content" required>
              <WangEditor v-if="open" v-model="form.content"></WangEditor>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">保 存</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog
      title="业务办理"
      v-model="openDetail"
      width="45vw"
      align-center
      append-to-body
    >
      <el-form
        ref="detailRef"
        :model="detailForm"
        :rules="rules"
        label-width="110px"
      >
        <el-row>
          <el-col :span="14">
            <el-form-item label="事项名称">{{ form.itemName }}</el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="浏览量">{{ form.viewCount }}</el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="事项类型">{{ form.itemType }}</el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="是否常用事项">
              <el-tag type="primary" v-if="form.status == 1">是</el-tag>
              <el-tag type="info" v-if="form.status == 0">否</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="办理地址">{{ form.processingAddress }}</el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="logo">
              <el-image
                style="width: 60px; height: 60px"
                :src="[baseUrl + form.logoUrl]"
                :preview-src-list="baseUrl + form.logoUrl"
              >
              </el-image>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
              <div class="rich-text-container" v-html="form.content"></div
            ></el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="close">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WorkGuide">
import {
  delWorkGuide,
  addWorkGuide,
  updateWorkGuide,
  listWorkGuide,
  getWorkGuideById,
} from "@/api/other/workGuide";

const { proxy } = getCurrentInstance();
const baseUrl = import.meta.env.VITE_APP_BASE_API;

const tableList = ref([]);
const open = ref(false);
const openDetail = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const detailForm = ref();
const options = ref([
  {
    value: "落户办理",
    label: "落户办理",
  },
  {
    value: "户口登记",
    label: "户口登记",
  },
  {
    value: "居住证业务",
    label: "居住证业务",
  },
  {
    value: "开具证明",
    label: "开具证明",
  },
  {
    value: "其他",
    label: "其他",
  },
]);
const type_options = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    itemName: undefined,
    itemType: "",
  },
  rules: {
    itemName: [
      { required: true, message: "事项名称不能为空", trigger: "blur" },
    ],
    itemType: [
      { required: true, message: "事项类型不能为空", trigger: "blur" },
    ],
    content: [{ required: true, message: "事项内容不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询角色列表 */
function getList() {
  type_options.value = [...[{ value: "", label: "全部" }], ...options.value];
  loading.value = true;
  listWorkGuide(queryParams.value).then((response) => {
    const { data } = response;
    tableList.value = data.records ?? [];
    total.value = data.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    itemName: undefined,
    status: 0,
    itemType: undefined,
    logoUrl: undefined,
    content: undefined,
    processingAddress: undefined,
  };
  proxy.resetForm("workGuideRef");
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加业务办理";
}

/** 查询按钮操作 */
function handleDetail(row) {
  getWorkGuideById({ id: row.id, type: "admin" }).then((response) => {
    form.value = response.data;
    openDetail.value = true;
  });
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  getWorkGuideById({ id: row.id, type: "admin" }).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "业务办理编辑";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["workGuideRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateWorkGuide(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWorkGuide(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm("确认删除该数据", "删除确认")
    .then(function () {
      return delWorkGuide(row.id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

function close() {
  openDetail.value = false;
  reset();
}

getList();
</script>
<style lang="scss" scoped>
:deep(.el-table td.el-table__cell div) {
  white-space: wrap;
  overflow: hidden;
  -webkit-line-clamp: 2;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
}
</style>
